package com.linfeng.business.admin.web;

import com.linfeng.component.common.exception.CustomException;
import com.linfeng.component.common.exception.CustomExceptionCode;
import com.linfeng.component.common.response.ResponseEntity;
import com.linfeng.component.oauth2.tools.TokenTools;
import com.linfeng.component.tools.MqTools;
import com.linfeng.component.tools.RedisTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.common.exceptions.InvalidTokenException;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.ResourceServerTokenServices;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import org.springframework.security.oauth2.provider.token.store.redis.RedisTokenStore;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
@RefreshScope
public class IndexController {

    @GetMapping(value = "/v1/index")
    public ResponseEntity index() {
        new CustomException(CustomExceptionCode.LOGIN_FAIL);
        return ResponseEntity.body("index");
    }

    @Value("${jwt.filter_url:}")
    private String filterUrl;

    @GetMapping(value = "/v1")
    public String config() {

        return filterUrl;
    }

    @Autowired
    ResourceServerTokenServices resourceServerTokenServices;

    @Autowired
    JwtAccessTokenConverter jwtAccessTokenConverter;

    @Autowired
    RedisConnectionFactory redisConnectionFactory;

    @Resource
    RedisTools redisTools;


    @GetMapping(value = "/v1/token/parse")
    public ResponseEntity parseToken(HttpServletRequest request) {

        String authorization = request.getHeader("Authorization");
        String value = authorization.substring("bearer ".length());
        Map<String, Object> response = new HashMap<>();
        RedisTokenStore tokenStore = new RedisTokenStore(redisConnectionFactory);
        OAuth2AccessToken token = tokenStore.readAccessToken(value);
        if (token == null) {
            throw new InvalidTokenException("Token was not recognised");
        } else if (token.isExpired()) {
            throw new InvalidTokenException("Token has expired");
        } else {
            OAuth2Authentication authentication = this.resourceServerTokenServices.loadAuthentication(token.getValue());
            response = (Map<String, Object>) this.jwtAccessTokenConverter.convertAccessToken(token, authentication);
            response.put("active", true);

        }

        return ResponseEntity.body(response);
    }

    @GetMapping(value = "/v1/token")
    public ResponseEntity getToken() {

        return ResponseEntity.body(redisTools.get("token"));
    }

    @Autowired
    MqTools mqTools;

    @GetMapping(value = "/v1/mq/send")
    public ResponseEntity sendMq() {

        redisTools.incr("mqtest", 1);
        Object mqtest = redisTools.get("mqtest");
        mqTools.send(mqtest);

        return ResponseEntity.body(mqtest);
    }
}
